package com.rede.didiok.community.web;

import java.util.Collection;
import java.util.List;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.dto.community.MomentCommentDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;
import com.rede.didiok.community.service.MomentCommentService;



/**
 * 用户动态评价表
 *
 * @author Rede
 * @email didiok@foxmail.com
 * @date 2023-09-13 08:18:34
 */
@Slf4j
@RestController
@RequestMapping("community/feign/momentComment")
public class MomentCommentController {
    @Autowired
    private MomentCommentService momentCommentService;


    @ApiOperation(value = "获取评论列表", notes = "获取评论列表", response = String.class)
    @PostMapping(value = "/getList")
    public String getList(@Validated({GetList.class}) @RequestBody MomentCommentDto momentCommentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取评论列表: {}", momentCommentDto);
        return ResultUtil.successWithData(momentCommentService.getPageList(momentCommentDto));
    }

    @AvoidRepeatableCommit
    @ApiOperation(value = "增加评论", notes = "增加评论", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody MomentCommentDto momentCommentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("新增评论: {}", momentCommentDto);
        return ResultUtil.successWithData(momentCommentService.addComment(momentCommentDto));
    }

    @ApiOperation(value = "编辑评论", notes = "编辑评论", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody MomentCommentDto momentCommentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑评论: {}", momentCommentDto);
        return ResultUtil.successWithMessage(momentCommentService.editComment(momentCommentDto));
    }

    @ApiOperation(value = "删除评论", notes = "删除评论", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody MomentCommentDto momentCommentDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("删除评论: {}", momentCommentDto);
        return ResultUtil.successWithMessage(momentCommentService.deleteComment(momentCommentDto));
    }

    @ApiOperation(value = "删除选中评论", notes = "删除选中评论", response = String.class)
    @PostMapping("/deleteBatch")
    public String deleteBatch(@Validated({Delete.class}) @RequestBody List<MomentCommentDto> commentDtoList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.successWithMessage(momentCommentService.deleteBatchComment(commentDtoList));
    }

    @ApiOperation(value = "审核评论", notes = "审核评论", response = String.class)
    @PostMapping("/audit")
    public String audit(@RequestBody MomentCommentDto momentCommentDto) {
        return ResultUtil.successWithMessage(momentCommentService.auditComment(momentCommentDto));
    }
    /**
     * 根据id获取榜单成员评论
     * @param
     * @return
     */
    @GetMapping("/getCommentById")
    R getCommentById(@RequestParam("uid") String uid){
        return R.ok().setData(momentCommentService.getCommentById(uid));
    }
    /**
     * 评论下架
     * @param
     * @return
     */
    @PostMapping("/revocation")
    R revocationComment(@RequestParam("uid") String uid){
        return R.ok().setData(momentCommentService.revocationComment(uid));
    }

    /**
     * 获取评论数量
     * @param createUserUid
     * @param
     * @param toUserUid
     * @return
     */
    @GetMapping("/getCommentCount")
    Integer getCommentCount(@RequestParam("createUserUid")String createUserUid, @RequestParam("momentUid")String momentUid, @RequestParam("toUserUid")String toUserUid){
        return momentCommentService.getCommentCount(createUserUid, momentUid, toUserUid);
    }

    /**
     * 根据id集合查询评论
     * @param uidList
     * @return
     */
    @GetMapping("/getCommentPageListByIds")
    String getCommentPageListByIds(@RequestParam("uidList") Collection<String> uidList){
        return ResultUtil.successWithData(momentCommentService.getCommentPageListByIds(uidList));
    }

    /**
     * 添加评论（当该评论不是一级评论时，需要设置一级评论UID字段）
     * @param comment
     * @return
     */
    @PostMapping("/addCommentWithFirstCommentUid")
    R addCommentWithFirstCommentUid(@RequestBody MomentCommentDto comment){
        return R.ok().setData(momentCommentService.addCommentWithFirstCommentUid(comment));
    }
}
